package com.zly.lab.blog.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Slf4j
@Controller
public class UserController {
//    @GetMapping("/")
//    public String redirect(HttpServletRequest request, HttpServletResponse response){
//        return "forward:/login";
//    }
    @GetMapping("/login")
    public String login(HttpServletRequest request, Map map){
        //保存原始访问路径:
        String referer = request.getHeader("Referer");//获取原始访问路径
        String url = request.getParameter("url");//获取原始访问路径
        log.info("Referer:{}",referer);
        log.info("Url:{}",url);
        //1 先判断保存的url
        if(url!=null&&url!=""){
            //使用url作为原始访问路径
            map.put("url",url);
        }else if(referer!=null&&referer.contains("login")){
            //2 如果头信息中有login字段(是从登录-->登录)
            map.put("url","");
        }else {
            //3 头信息作为原始访问路径
            map.put("url",referer);
        }
        return "comm/login";
    }
    @RequestMapping("/toRegister")
    public String toRegister(){
        return  "comm/myregister";
    }
    @GetMapping("/errorPage/comm/{errnum}")
    public String errorPage(@PathVariable("errnum")String errnum){
        return "comm/"+errnum;
    }
}
